package getsmart.project.activity;

import getsmart.project.R;
import getsmart.project.database.Bintang;
import getsmart.project.database.DBStudyType;
//import getsmart.project.database.LogActivity;

import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.Toast;

public class ListBab extends Activity implements View.OnClickListener{
	public int tempID = 1;
	int [] bintangTarget;
	int kelasAnak = 0;
	public int getSize;
	String [] arrData;
	Button [] profName;
	DBStudyType dbPelajaran;
	//LogActivity logAct;
	Bintang dataStar;
	EditText pertanyaan, kategori;
	TableLayout tabel4data;// tabel for data

	public static int idBab = 0;
	private static void getKelasSiswa(int bab){
		ListBab.idBab = bab;
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_bab);
		kelasAnak = ChooseKelas.kelasSiswa;
		dataStar = new Bintang(this); 
		dbPelajaran = new DBStudyType(this);
		//logAct = new LogActivity(this);
		tabel4data = (TableLayout) findViewById(R.id.table_bab_kelas);
		
		viewData();
		Toast.makeText(getBaseContext()," Jumlah data" + getSize,
				Toast.LENGTH_LONG).show();
	}
	protected void viewData() {
		while (tabel4data.getChildCount() > 1) {
			tabel4data.removeViewAt(1);
		}
 
		ArrayList<ArrayList<Object>> data = dbPelajaran.getProfile(ChooseKelas.kelasSiswa);//
		if(data.size()==0){
			dbPelajaran.generateData(ChooseKelas.kelasSiswa);
		}
		int k = 0;
		int kl = 0;
		k = ChooseKelas.kelasSiswa;
		kl = dbPelajaran.getBabUsingStudyType(k);
		bintangTarget = new int[kl];
		getSize = kl;
		for (int posisi = 0; posisi <kl; posisi++) {
			TableRow tabelBaris = new TableRow(this);
			TableRow tabelBaris2 = new TableRow(this);
			TableRow tabelBaris3 = new TableRow(this);
			bintangTarget[posisi] = tempID*4;
			Button[] profName = new Button[kl];
			profName[posisi] = new Button(this);
			profName[posisi].setBackgroundResource(R.drawable.button_lainjuga);
			profName[posisi].setText(" Bab "+tempID);
			if(posisi>0){
				if(bintangTarget[posisi]>dataStar.getJumlah(ListProfil.profileID, posisi, kelasAnak, ListBelajar.idBacaan)){
					profName[posisi].setEnabled(false);
				}
			}
			profName[posisi].setOnClickListener(this);
			profName[posisi].setId(tempID);
			tempID++;
			tabelBaris.addView(profName[posisi]);					
			tabel4data.addView(tabelBaris);
		}
	}
	@Override
	public void onClick(View v){
		
		Button b = (Button)v;
		for(int i = 0; i<getSize;i++){
			if(b.getId() == i){
				idBab = i;
				
				if(ChooseMenu.MenuPilihan == "Belajar"){
					//logAct.addLog(ListProfil.profileID, "Mengerjakan test membaca");
					Intent inte = new Intent(ListBab.this,ChooseBelajar.class);
					//logAct.close();
					startActivity(inte);					
				}
				if(ChooseMenu.MenuPilihan == "Test"){
					
					if(ListBelajar.typeStudy.equals("Membaca")){
						Toast.makeText(getBaseContext(), "pilihan di klik " + ChooseMenu.MenuPilihan + "List yang di dapat" + ListBelajar.typeStudy,
								Toast.LENGTH_LONG).show();
						//logAct.addLog(ListProfil.profileID, "Mengerjakan test Membaca");
						//logAct.close();
						if(ChooseKelas.kelasSiswa == 1){
							Intent inte = new Intent(ListBab.this,TestActivityMembaca.class);
							startActivity(inte);
						}
						
					}
					if(ListBelajar.typeStudy.equals("Berhitung")){
						
						//logAct.addLog(ListProfil.profileID, "Mengerjakan test Matematika");
						//logAct.close();
						if(ChooseKelas.kelasSiswa == 1){
							Intent inte = new Intent(ListBab.this,PlayWithMatemathic.class);
							startActivity(inte);
						}
						if(ChooseKelas.kelasSiswa == 2){
							Intent inte = new Intent(ListBab.this,PlayMathematic2.class);
							startActivity(inte);
						}
						if(ChooseKelas.kelasSiswa == 3){
							Intent inte = new Intent(ListBab.this,PlayWithMathematic3.class);
							startActivity(inte);
						}
					}
					
				}
				if(ChooseMenu.MenuPilihan == "Statistik"){
					Intent inte = new Intent(ListBab.this,StatistikAnak.class);
					startActivity(inte);
				}
			}
		}
	}
}
